<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // GET
    var obj = {
        type: "get",
        url: "../php/isExistUser.php",
        // data: "user=wwww&pwd=123132",
        data: {
            user: "wwww",
            pwd: 123132
        },
        dataType: "json",
        callback: function (result) {
            console.log(result);
        }
    }

    get(obj);

    function get(option = { type: "get", data: "" }) {
        var req = new XMLHttpRequest();
        if (typeof option.data == "object") {   //如果是对象  遍历对象 拼接成 字符串
            /* {
                user: "wwww",
                pwd: 123132
               }   
            =>  user=wwww&pwd=123132  */
            var str = "";
            for (var key in option.data) {
                console.log(key, option.data[key]);//  key => "user"/"pwd"
                str += key + "=" + option.data[key] + "&";
            }
            str = str.substr(0, str.length - 1);

        } else {
            str = option.data;
        }
        console.log(str);
        req.open(option.type, option.url + "?" + str, true);
        req.send();
        req.onreadystatechange = function () {
            if (req.readyState == 4 && req.status == 200) {
                var result = req.responseText;
                if (option.dataType == "json") {
                    result = JSON.parse(result);
                }
                if (option.callback) {
                    option.callback(result);
                }
            }
        }
    }
</script>

</html>